package portlet;

import java.io.IOException;

import java.sql.Timestamp;

import java.text.SimpleDateFormat;

import java.util.Date;
import java.util.Locale;
import java.util.ResourceBundle;

import javax.el.ELContext;
import javax.el.ExpressionFactory;
import javax.el.ValueExpression;

import javax.faces.context.ExternalContext;
import javax.faces.context.FacesContext;

import javax.portlet.PortletRequest;

import oracle.adf.model.BindingContext;
import oracle.adf.model.binding.DCBindingContainer;
import oracle.adf.model.binding.DCIteratorBinding;

import oracle.binding.BindingContainer;

import oracle.jbo.ViewObject;

public class Utils {
    public Utils() {
        super();
    }
    
    public static Locale getPortalLocale(String lang) {
        Locale portalLocale = new Locale("vi", "VN");
        String language = "";
        try {
            if (getPortletRequest().getParameter(lang) != null) {
                language = getPortletRequest().getParameter(lang);
                portalLocale = new Locale(language);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return portalLocale;
    }

    public static String getResource(String resourceKey, String lang) {
        Locale locale = getPortalLocale(lang);
        ResourceBundle bundle =
            ResourceBundle.getBundle("portlet.resource.PortletsBundle",
                                     locale);
        return bundle.getString(resourceKey);
    }

    public static PortletRequest getPortletRequest() {
        PortletRequest req =
            (PortletRequest)FacesContext.getCurrentInstance().getExternalContext().getRequest();
        return req;
    }

    public static void redirect(String url) {
        ExternalContext externalContext =
            FacesContext.getCurrentInstance().getExternalContext();
        try {
            externalContext.redirect(url);
        } catch (IOException e) {
        }
    }

    public static oracle.jbo.domain.Date getCurrentDate() {
        Date now = new Date();
        Timestamp timestamp = new Timestamp(now.getTime());
        oracle.jbo.domain.Date jboDate = new oracle.jbo.domain.Date(timestamp);
        return jboDate;
    }

    public static DCIteratorBinding getIteratorBinding(String iteratorName) {
        return getDCBindingContainer().findIteratorBinding(iteratorName);
    }

    public static DCBindingContainer getDCBindingContainer() {
        return (DCBindingContainer)getBindingContainer();
    }

    public static BindingContainer getBindingContainer() {
        return (BindingContainer)getElExpression("#{bindings}");
    }

    /**
     * @param el
     * @return
     */
    public static Object getElExpression(String el) {
        FacesContext facesContext = FacesContext.getCurrentInstance();
        ELContext elContext = facesContext.getELContext();
        ExpressionFactory expressionFactory =
            facesContext.getApplication().getExpressionFactory();
        ValueExpression valueExp =
            expressionFactory.createValueExpression(elContext, el,
                                                    Object.class);
        return valueExp.getValue(elContext);
    }

    /**
     * @param el
     * @param val
     */
    public static void setElExpression(String el, Object val) {
        FacesContext facesContext = FacesContext.getCurrentInstance();
        ELContext elContext = facesContext.getELContext();
        ExpressionFactory expressionFactory =
            facesContext.getApplication().getExpressionFactory();
        ValueExpression exp =
            expressionFactory.createValueExpression(elContext, el,
                                                    Object.class);
        exp.setValue(elContext, val);
    }

    /**
     * Refresh a View Iterator
     * @param iteratorName
     */
    public static void iteratorRefresh(String iteratorName) {
        try {
            DCBindingContainer dcBinding =
                (DCBindingContainer)BindingContext.getCurrent().getCurrentBindingsEntry();
            DCIteratorBinding ViewIterator =
                (DCIteratorBinding)dcBinding.get(iteratorName);
            ViewIterator.getViewObject().clearCache();
            int oldMode = ViewIterator.getViewObject().getQueryMode();
            ViewIterator.getViewObject().setQueryMode(ViewObject.QUERY_MODE_SCAN_DATABASE_TABLES);
            ViewIterator.executeQuery();
            ViewIterator.getViewObject().setQueryMode(oldMode);
            ViewIterator.refresh(DCIteratorBinding.RANGESIZE_UNLIMITED);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
